package com.imooc.miaosha.controller;

import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.domain.OrderInfo;
import com.imooc.miaosha.service.RedisService;
import com.imooc.miaosha.result.Const;
import com.imooc.miaosha.result.Result;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.MiaoshaUserService;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.vo.MiaoshaGoodsVo;
import com.imooc.miaosha.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Paul
 * @version 0.1
 * @date 2018/3/20 0020
 */
@Controller
@RequestMapping("order")
public class OrderController {
    private final MiaoshaUserService userService;
    private final RedisService redisService;
    private final GoodsService goodsService;
    private final OrderService orderService;
    @Autowired
    public OrderController(MiaoshaUserService userService, RedisService redisService, GoodsService goodsService, OrderService orderService) {
        this.userService = userService;
        this.redisService = redisService;
        this.goodsService = goodsService;
        this.orderService = orderService;
    }

    @ResponseBody
    @RequestMapping("detail")
    public Result<OrderDetailVo> info(MiaoshaUser user,
                                      @RequestParam("orderId") long orderId) {

        if (user == null) {
            return Result.error(Const.SESSION_ERROR);
        }
        OrderInfo orderInfo = orderService.getOrderInfoByUserIdAndOrderId(user.getId(), orderId);
        if (orderInfo == null) {
            return Result.error(Const.ORDER_NOT_EXIST);
        }
        long goodsId = orderInfo.getGoodsId();
        MiaoshaGoodsVo miaoshaGoodsVo = goodsService.getGoodsVoByGoodsId(goodsId);
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setGoods(miaoshaGoodsVo);
        orderDetailVo.setOrder(orderInfo);
        return Result.success(orderDetailVo);

    }
}
